<?php
/**
 * Ham tao danh sach trang
 * @param doi tuong cua lop Utilities_Paginator
 */
if (!function_exists('pageslist'))
{
    function pageslist($paginator)
    {
    	$totalItemCount    = $paginator->getTotalItemCount();
    	$currentPageNumber = $paginator->getCurrentPageNumber();
    	$first = $paginator->getFirst();
    	$last = $paginator->getLast();
    	$previous = $paginator->getPrevious();
    	$next = $paginator->getNext();
    	$itemCountPerPage = $paginator->getItemCountPerPage();
    	$currentItemCount = $paginator->getCurrentItemCount();
    	
    	$itemStart = ($currentPageNumber - 1)*$itemCountPerPage + 1;
    	$itemEnd   = $itemStart + $currentItemCount - 1;

	    $from = $currentPageNumber - 1;
		$to = $currentPageNumber + 1;
		if ($from < $first) {
	    	$from = $first;
	    	$to = $from + 2;
	    	$to = $to <= $last ? $to : $last;
		}
		if ($to > $last) {
	    	$to = $last;
	    	$from = $to - 2;
	    	$from = $from >= $first ? $from : $first;
		}
		
		echo '<div id="pageslist">
     			<span>'.$itemStart."-".$itemEnd.' trong tổng số '.$totalItemCount.'</span>'."\n";
		
		if($previous == null)
		      echo '<span class="disabled">&laquo;&laquo; Trang đầu</span>
     				<span class="disabled">&laquo; Trước</span>'."\n";
		else 
			  echo '<a href="#p'.$first.'">&laquo;&laquo; Trang đầu</a>
     				<a href="#p'.$previous.'">&laquo; Trước</a>'."\n";
		
	    for ($i = $from; $i <= $to; $i++) {
	    	if($i != $currentPageNumber)
	    		echo '<a href="#p'.$i.'">'.$i.'</a>'."\n";
	    	else 
	    		echo '<span class="selected">'.$i.'</span>'."\n";	 	
	    }
	    
	    if($next == null)
		      echo '<span class="disabled">Tiếp &raquo;</span>
     				<span class="disabled">Trang cuối &raquo;&raquo;</span>'."\n";
		else 
			  echo '<a href="#p'.$next.'">Tiếp &raquo;</a>
     				<a href="#p'.$last.'">Trang cuối &raquo;&raquo;</a>'."\n";
		
	    echo '</div>'."\n";
    }
}